<template>
  <div>
    <el-form ref="editFormRef" :rules="rules" :model="editForm.data" label-width="120">
      <el-form-item label="运营商" prop="operator">
        <el-select
          v-model="editForm.data.operator"
          filterable
          style="width: 320px"
          placeholder="请选择运营商"
        >
          <el-option
            v-for="item in operator.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="编码" prop="iccid">
        <el-input
          v-model="editForm.data.iccid"
          disabled
          style="width: 320px"
          maxlength="20"
          placeholder="请输入编码"
        />
      </el-form-item>

      <el-form-item label="到期时间" prop="expiresIn">
        <el-date-picker
          v-model="editForm.data.expiresIn"
          style="width: 320px"
          type="date"
          placeholder="请选择到期时间"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="流量包" prop="flux">
        <el-input
          v-model="editForm.data.flux"
          style="width: 320px"
          placeholder="请输入流量包"
        >
          <template #append>G</template></el-input
        >
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="editForm.data.remark"
          style="width: 320px"
          placeholder="请输入备注"
        />
      </el-form-item>
      <el-form-item>
        <div class="footer">
          <div></div>
          <div>
            <el-button class="reset-btn" @click="resetForm(editFormRef)">取 消</el-button>
            <el-button class="confirm-btn" @click="submitForm(editFormRef)"
              >确 认</el-button
            >
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
const emits = defineEmits(["operationDone"]);
import { getEditSIM } from "../request.js";
import { getDictionary } from "@/service/modules/publicModule";

const props = defineProps({
  operationId: {
    type: Object,
    default: () => {},
  },
});

const editFormRef = ref("");
const editForm = reactive({
  data: {
    simId: props.operationId.simId,
    operator: props.operationId.operator,
    iccid: props.operationId.iccid,
    expiresIn: props.operationId.expiresIn,
    flux: props.operationId.flux,
    remark: props.operationId.remark,
  },
});

const operator = reactive({
  data: [],
});

const getData = async () => {
  await getDictionary("network_operator").then((res) => {
    operator.data = res.data.result;
  });
};

getData();

const rules = reactive({
  operator: [{ required: true, message: "请选择运营商", trigger: "change" }],
  iccid: [{ required: true, message: "请输入编码", trigger: "blur" }],
  expiresIn: [{ required: true, message: "请选择到期时间", trigger: "change" }],
  flux: [{ required: true, message: "请输入流量包", trigger: "blur" }],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      getEditSIM(editForm.data).then((res) => {
        if (res.data.code === 0) {
          ElMessage({
            type: "success",
            message: res.data.result,
          });
          emits("operationDone");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  editForm.data = {};
  formEl.resetFields();
  emits("operationDone");
};
</script>

<style lang="less" scoped>
.footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.reset-btn {
  width: 65px;
  height: 32px;
  background: #ffffff;
  border-radius: 2px;
  border: 1px solid #d9d9d9;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
}
.confirm-btn {
  width: 65px;
  height: 32px;
  background: #007aff;
  border-radius: 2px;
  font-weight: 400;
  font-size: 14px;
  color: #ffffff;
}
</style>
